﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarSF.StudentInfosManager
{
    /// <summary>
    /// BaseInfosInputer.xaml 的交互逻辑
    /// </summary>
    public partial class BaseInfosInputer : UserControl
    {
        public BaseInfosInputer()
        {
            InitializeComponent();

            this.cmbStudentGrade.DataContext =
                this.tbxStudentClass.DataContext =
                this.tbxStudentNumber.DataContext =

                this.tbxStudentName.DataContext =
                this.tbxStudentOldName.DataContext =
                this.tbxCid.DataContext =

                this.tbxSchoolRoll.DataContext =
                this.cmbAdmissionDate.DataContext =
                this.cmbStudentNation.DataContext =

                this.cmbPoliticalStatus.DataContext =
                this.lsNativePlace.DataContext =
                this.lsResidencePlace.DataContext =

                this.cmbResidenceType.DataContext =
                this.cmbStudentHealthCondition.DataContext =
                this.lsStudentCurrentPlace.DataContext =

                this.tbxStudentDetailAddress.DataContext =
                this.tbxPostCode.DataContext =
                this.tbxStudentEmail.DataContext =

                this.tbxStudentPhoneCode.DataContext =
                this.tbxGuardianName.DataContext =
                this.tbxGuardianCompany.DataContext =

                this.tbxGuardianCid.DataContext =
                this.tbxGuardianRelation.DataContext =
                this.tbxGuardianPhoneCode.DataContext =


                //TODO: 每新加一个字段，都要在这里添加

                infosEntry;

            infosEntry.NativePlaceChanged += InfosEntry_NativePlaceChanged;
            infosEntry.ResidencePlaceChanged += InfosEntry_ResidencePlaceChanged;
            infosEntry.StudentCurrentPlaceChanged += InfosEntry_StudentCurrentPlaceChanged;
        }

        private void InfosEntry_StudentCurrentPlaceChanged(object sender, StudentBaseInfos.PlaceChangedEventArgs e)
        {
            lsStudentCurrentPlace.Text = e.NewPlace;
        }

        private void InfosEntry_ResidencePlaceChanged(object sender, StudentBaseInfos.PlaceChangedEventArgs e)
        {
            lsResidencePlace.Text = e.NewPlace;
        }

        private void InfosEntry_NativePlaceChanged(object sender, StudentBaseInfos.PlaceChangedEventArgs e)
        {
            lsNativePlace.Text = e.NewPlace;
        }

        public bool ErrorExist
        {
            get
            {
                foreach (UIElement ue in gridBaseInfos.Children)
                {
                    var tbx = ue as TextBox;
                    if (tbx == null || string.IsNullOrWhiteSpace(tbx.Text)) continue;

                    if (Validation.GetErrors(tbx).Count > 0) return true;
                }
                return false;
            }
        }

        private StudentBaseInfos infosEntry = new StudentBaseInfos();
        /// <summary>
        /// 表示一个学生的信息。
        /// </summary>
        public StudentBaseInfos InfosEntry
        {
            get { return infosEntry; }
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                LMessageBox.Show(InfosEntry.ToString(), Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else if (e.Key == Key.F3)
            {
                InfosEntry.NativePlace = "江苏省盐城市射阳县";
                lsNativePlace.Text = "江苏省盐城市射阳县";
            }
            else if (e.Key == Key.F4)
            {
                InfosEntry.PoliticalStatus = "党员";
            }
        }

        private void lsNativePlace_TextChanged(object sender, TextChangedEventArgs e)
        {
            infosEntry.SetNativePlace(lsNativePlace.Text);
        }

        private void lsResidencePlace_TextChanged(object sender, TextChangedEventArgs e)
        {
            infosEntry.SetResidencePlace(lsResidencePlace.Text);
        }

        private void lsStudentCurrentPlace_TextChanged(object sender, TextChangedEventArgs e)
        {
            infosEntry.SetStudentCurrentPlace(lsStudentCurrentPlace.Text);
        }

        internal void ClearFields()
        {
            this.cmbStudentGrade.SelectedIndex = -1;
            this.tbxStudentClass.Text =
            this.tbxStudentNumber.Text =

            this.tbxStudentName.Text =
            this.tbxStudentOldName.Text =
            this.tbxCid.Text =

            this.tbxSchoolRoll.Text = "";
            this.cmbAdmissionDate.SelectedIndex =
            this.cmbStudentNation.SelectedIndex =

            this.cmbPoliticalStatus.SelectedIndex = -1;
            this.lsNativePlace.Text =
            this.lsResidencePlace.Text = "";

            this.cmbResidenceType.SelectedIndex =
            this.cmbStudentHealthCondition.SelectedIndex = -1;
            this.lsStudentCurrentPlace.Text =

            this.tbxStudentDetailAddress.Text =
            this.tbxPostCode.Text =
            this.tbxStudentEmail.Text =

            this.tbxStudentPhoneCode.Text =
            this.tbxGuardianName.Text =
            this.tbxGuardianCompany.Text =

            this.tbxGuardianCid.Text =
            this.tbxGuardianRelation.Text =
            this.tbxGuardianPhoneCode.Text = "";
        }
    }
}
